package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {
        Dish dish = Dish.builder()
                .status(StatusConstant.ENABLE)
                .build();
//        查询已起售的商品数量
        Integer enable = dishMapper.overviewDishesAmount(dish);
//        查询已停售的商品数量
        dish.setStatus(StatusConstant.DISABLE);
        Integer disable = dishMapper.overviewDishesAmount(dish);
        return new DishOverViewVO(enable, disable);
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {
        Setmeal setmeal = Setmeal.builder()
                .status(StatusConstant.ENABLE)
                .build();
//        查询已起售的套餐数量
        Integer enable = setmealMapper.overviewSetmealsAmount(setmeal);
//        查询已停售的套餐数量
        setmeal.setStatus(StatusConstant.DISABLE);
        Integer disable = setmealMapper.overviewSetmealsAmount(setmeal);
        return new SetmealOverViewVO(enable, disable);
    }

    /**
     * 查询订单管理总览
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        Orders orders = new Orders();
//        查询全部订单
        Integer allOrders = orderMapper.overviewOrdersAmount(orders);
//        查询已取消订单
        orders.setStatus(Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.overviewOrdersAmount(orders);
//        查询已完成订单
        orders.setStatus(Orders.COMPLETED);
        Integer completedOrders = orderMapper.overviewOrdersAmount(orders);
//        查询待派送订单
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = orderMapper.overviewOrdersAmount(orders);
//        查询待接单订单
        orders.setStatus(Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.overviewOrdersAmount(orders);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 查询今日运营数据
     * @return
     */
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
//        LocalDate now = LocalDate.now();
//        LocalDateTime begin = LocalDateTime.of(now, LocalTime.MIN);
//        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
//        查询有效订单数
        Integer validOrderCount = reportMapper.selectOrderCount(begin, end);
//        查询订单完成率
        Integer allOrdersAmount = reportMapper.selectAllOrdersAmount();
        double orderCompletionRate = new BigDecimal((float) validOrderCount / allOrdersAmount).setScale(2, RoundingMode.HALF_UP).doubleValue();
//        查询营业额
        BigDecimal bigDecimal = reportMapper.selectByDateNow(begin, end, Orders.COMPLETED);
        bigDecimal = bigDecimal == null ? BigDecimal.valueOf(0.0) : bigDecimal;
        double turnover = bigDecimal.doubleValue();
//        查询平均客单价
        double unitPrice;
        if (turnover != 0) {
            unitPrice = turnover / validOrderCount;
        }else {
            unitPrice = 0.0;
        }
//        查询新用户数
        Integer newUsers = reportMapper.selectUserNow(begin, end);
        newUsers = newUsers == null ? 0 : newUsers;
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .turnover(turnover)
                .build();
    }
}
